/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UGAME_DATA_REL_SESSION_H_
#define UGAME_DATA_REL_SESSION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <string>
#include <vector>
#include <memory>
#include <boost/unordered_map.hpp>
#include <UGame/Macros.h>
#include <UGame/Base/AtomicValue.h>
#include <UGame/System/Mutex.h>
#include <UGame/Data/Rel/SqlPreparedStatement.h>

namespace UGame {
namespace Data {
namespace Rel {
    class QueryResult;
    class SqlResultQueue;
    class SqlDelayThread;

    class Session
    {
    public:
        const static int MAX_QUERY_LENGTH = 1024 * 32;
        typedef void(*ERROR_HANDLER)(int, const char* _Err, ...);

        Session(void);
        ~Session(void);

        bool Open(const std::string& _Key, int _QueryConns, 
            const std::string& _Host,  const std::string& _Port, 
            const std::string& _User,  const std::string _Password, 
            const std::string& _Db);

        void Close(void);
        void EscapeString(std::string& _Sql);
        void Ping(void);

        std::string GetServerInfo(void);
        std::string GetClientInfo(void);

        void SetErrorHandler(ERROR_HANDLER _Handler);

        QueryResult* PQuery(const char* _Sql, ...);
        bool PExecute(const char* _Sql, ...);
        SqlStatement CreateStatement(SqlStatementID& _ID, const char* _Format);
        bool ExecuteStatement(const SqlStatementID& _ID, SqlStmtParameters* _Params);
        bool DirectExecuteStatement(const SqlStatementID& _ID, SqlStmtParameters* _Params);
        std::string GetStatementString(const int _ID);

        void ProcessAsyncResultQueue(void);

        template<class Class, typename ParamType1>
        bool AsyncQuery(Class* _Object, void (Class::*_Method)
            (QueryResult*, ParamType1), ParamType1 _Param1, const char* _Sql);

        template<class Class, typename ParamType1>
        bool AsyncPQuery(Class* _Object, void (Class::*_Method)
            (QueryResult*, ParamType1), ParamType1 _Param1, const char* _Format, ...);
        
    private:
        friend class SqlStatement;
        friend class SqlConnection;
        ERROR_HANDLER _GetErrorHandler(void) const { return _errHandler; }

        SqlConnection* _GetConnection(void);
        void _Uninitialize(void);
        typedef boost::unordered_map<std::string, int> PreparedStmtRegistry;

        bool _allowAsyncTrans;
        std::vector<SqlConnection*> _syncConns;
        int _syncConnUseIndex;
		System::Mutex _syncConnsLock;
        std::auto_ptr<SqlConnection> _asyncConn;
        PreparedStmtRegistry _stmtRegistry;
        mutable UGame::System::Mutex _stmtRegistryLock;
        int _stmtIndex;
        std::auto_ptr<SqlResultQueue> _resultQueue;
        std::auto_ptr<SqlDelayThread> _delayThread;
        ERROR_HANDLER _errHandler;
        DISALLOW_COPY_AND_ASSIGN(Session);
    };

}}}

#endif // UGAME_DATA_REL_SESSION_H_
